package com.ruoyi.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.user.mapper.UserBlackMapper;
import com.ruoyi.user.model.constant.UserConstant;
import com.ruoyi.user.model.pojo.entity.UserBlack;
import com.ruoyi.user.model.pojo.entity.UserDynamic;
import com.ruoyi.user.service.IUserBlackService;
import com.ruoyi.user.service.IUserDynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户黑名单 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-08-06
 */
@Service
public class UserBlackServiceImpl extends ServiceImpl<UserBlackMapper, UserBlack> implements IUserBlackService {


    @Autowired
    private IUserDynamicService userDynamicService;


    @Override
    public Integer userBlack(Long userId, Long aimUserId) {
        Assert.notNull(aimUserId, "对方为空");
        Long one = lambdaQuery().eq(UserBlack::getUserId, userId).eq(UserBlack::getAimUserId, aimUserId).count();
        if (one == 0) {
            UserBlack userBlack = new UserBlack();
            userBlack.setAimUserId(aimUserId);
            userBlack.setUserId(userId);
            userBlack.setIsUndock(UserConstant.IsUndock.XJ);
            save(userBlack);
            return 1;
        } else {
            remove(new QueryWrapper<UserBlack>().lambda().eq(UserBlack::getUserId, userId).eq(UserBlack::getAimUserId, aimUserId));
            return 0;
        }
    }

    @Override
    public Integer isUserBlack(Long userId, Long aimUserId) {
        Long one = lambdaQuery().eq(UserBlack::getUserId, userId).eq(UserBlack::getAimUserId, aimUserId).count();
        if (one > 0) return 1;
        return 0;
    }

    @Override
    public Set<Long> getUserBlackIds(Long userId) {
        return lambdaQuery().eq(UserBlack::getAimUserId, userId).eq(UserBlack::getIsUndock, UserConstant.IsUndock.XJ).list().stream().map(UserBlack::getUserId).collect(Collectors.toSet());
    }

    @Override
    public Set<Long> getUserBlackMeIds(Long userId) {
        return lambdaQuery().eq(UserBlack::getAimUserId, userId).eq(UserBlack::getIsUndock, UserConstant.IsUndock.XJ).list().stream().map(UserBlack::getUserId).collect(Collectors.toSet());
    }

    @Override
    public boolean getIsAimUserBlackIds(Long userId, Long aimUserId) {
        UserBlack one = lambdaQuery()
                .eq(UserBlack::getUserId, userId)
                .eq(UserBlack::getAimUserId, aimUserId)
                .eq(UserBlack::getIsUndock, UserConstant.IsUndock.XJ)
                .one();

        UserBlack userBlack = lambdaQuery()
                .eq(UserBlack::getUserId, aimUserId)
                .eq(UserBlack::getAimUserId, userId)
                .eq(UserBlack::getIsUndock, UserConstant.IsUndock.XJ)
                .one();

        UserDynamic one1 = userDynamicService.lambdaQuery().eq(UserDynamic::getUserId, userId).eq(UserDynamic::getAimUserId, aimUserId).one();
        if (!CommonUtils.isNull(one) || !CommonUtils.isNull(one1) ||!CommonUtils.isNull(userBlack)) return false;
        return true;
    }

}
